package com.wlw.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wlw.entity.City;
import com.wlw.entity.Foreign;
import com.wlw.entity.Province;
import com.wlw.service.CityService;
import com.wlw.service.ForeignService;
import com.wlw.service.ProvinceService;

import edu.xjsoft.base.orm.support.Page;
import edu.xjsoft.base.orm.support.PropertyFilter;

@Controller
@RequestMapping("/area/*")
public class AreaAction {
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private ForeignService foreignService;
	@Autowired
	private CityService cityService;
	
	@RequestMapping("addProvince")
	public String addProvince(HttpServletRequest req){
		System.out.println("加载至此，addProvince!");
		String pname = req.getParameter("pname");
		String direction = req.getParameter("direction");
		Province province = new Province();
		province.setDirection(direction);
		province.setName(pname);
		PropertyFilter filters = new PropertyFilter("name", pname);
		List<Province> provinces = provinceService.search(filters);
		if (provinces.size()==0) {
			provinceService.saveOrUpdate(province);
			return "background/success";
		}else {
			return "background/failure";
		}
	}
	
	@RequestMapping("addForeign")
	public String addForeign(HttpServletRequest req){
		String fname = req.getParameter("fname");
		String continent = req.getParameter("continent");
		Foreign foreign = new Foreign();
		foreign.setCountry(fname);
		foreign.setContinent(continent);
		PropertyFilter filters = new PropertyFilter("country", fname);
		List<Foreign> foreigns = foreignService.search(filters);
		if (foreigns.size()==0) {
			foreignService.saveOrUpdate(foreign);
			return "background/success";		
		}else {
			return "background/failure";
		}
	}
	@RequestMapping("findPF")
	public String findPF(Model model){
		List<Province> provinces = provinceService.loadAll();
		List<Foreign> foreigns = foreignService.loadAll();
		model.addAttribute("provinces", provinces);
		model.addAttribute("foreigns", foreigns);
		return "background/findArea";
	}
	@RequestMapping("modPro")
	public String modPro(HttpServletRequest req,Model model){
		int id = Integer.parseInt(req.getParameter("id"));
		/**
		 * 编码问题
		 */
		PropertyFilter filters = new PropertyFilter("id",id);
		List<Province> provinces = provinceService.search(filters);
		model.addAttribute("provinces", provinces);
		return "background/modProvince";
	}
	@RequestMapping("modProvince")
	public String modProvince(HttpServletRequest req){
		String name = req.getParameter("pname");
		String direction = req.getParameter("direction");
		int id = Integer.parseInt(req.getParameter("id"));
		PropertyFilter filters = new PropertyFilter("id", id);
		List<Province> provinces = provinceService.search(filters);
		provinces.get(0).setDirection(direction);
		provinces.get(0).setName(name);
		provinceService.saveOrUpdate(provinces.get(0));
		return "background/success";
	}
	@RequestMapping("delPro")
	public String delPro(HttpServletRequest req){
		int id = Integer.parseInt(req.getParameter("id"));
		provinceService.remove(id);
		return "background/successDel";
	}
	@RequestMapping("modFor")
	public String modFor(HttpServletRequest req,Model model){
		int id = Integer.parseInt(req.getParameter("id"));
		System.out.println("haha!");
		PropertyFilter filters = new PropertyFilter("id", id);
		List<Foreign> foreigns = foreignService.search(filters);
		model.addAttribute("foreigns", foreigns);
		return "background/modForeign";
	}
	@RequestMapping("modForeign")
	public String modForeign(HttpServletRequest req){
		String idstr = req.getParameter("id");
		int id = Integer.parseInt(idstr);
		PropertyFilter filters = new PropertyFilter("id", id);
		List<Foreign> foreigns = foreignService.search(filters);
		String continent = req.getParameter("continent");
		String country = req.getParameter("country");
		System.out.println("c:"+continent+"COU:"+country);
		foreigns.get(0).setContinent(continent);
		foreigns.get(0).setCountry(country);
		foreignService.saveOrUpdate(foreigns.get(0));
		return "background/success";
	}
	@RequestMapping("delFor")
	public String delFor(HttpServletRequest req){
		int id = Integer.parseInt(req.getParameter("id"));
		foreignService.remove(id);
		return "background/successDel";
	}
	@RequestMapping("modCity")
	public String modCity(HttpServletRequest req,Model model){
		int id = Integer.parseInt(req.getParameter("id"));
		PropertyFilter filters = new PropertyFilter("id", id);
		List<City> city = cityService.search(filters);
		model.addAttribute("city", city);
		return "background/modCity";
	}
	@RequestMapping("modifyCity")
	public String modifyCity(HttpServletRequest req){
		System.out.println("QQQ");
		int id = Integer.parseInt(req.getParameter("id"));
		PropertyFilter filters = new PropertyFilter("id", id);
		List<City> cities = cityService.search(filters);
		String consumeLevel = req.getParameter("consumeLevel");
		String custom = req.getParameter("custom");
		String dress = req.getParameter("dress");
		String history = req.getParameter("history");
		String hotView = req.getParameter("hotView");
		String language = req.getParameter("language");
		String name = req.getParameter("city");
		String traffic = req.getParameter("traffic");
		Integer perConsume = Integer.parseInt(req.getParameter("perConsume"));
		Integer prettyMonth = Integer.parseInt(req.getParameter("prettyMonth"));
		String way = req.getParameter("way");
		String map = req.getParameter("map");
		String fore = req.getParameter("country");
		String country_id = req.getParameter("country_id");
		if (country_id.length()!=0) {
			System.out.println();
			int f_id = Integer.parseInt(country_id);
			PropertyFilter filtersf = new PropertyFilter("id", f_id);
			List<Foreign> foreigns = foreignService.search(filtersf);
			foreigns.get(0).setCountry(fore);
			cities.get(0).setForeign(foreigns.get(0));
		}
		String province_id = req.getParameter("province_id");
		if (province_id.length()!=0) {
			int p_id = Integer.parseInt(province_id);
			PropertyFilter filtersp = new PropertyFilter("id", p_id);
			List<Province> provinces = provinceService.search(filtersp);
			String p_name = req.getParameter("province");
			provinces.get(0).setName(p_name);
			cities.get(0).setProvince(provinces.get(0));
		}
		cities.get(0).setConsumeLevel(consumeLevel);
		cities.get(0).setCustom(custom);
		cities.get(0).setDress(dress);
		cities.get(0).setHistory(history);
		cities.get(0).setHotView(hotView);
		cities.get(0).setLanguage(language);
		cities.get(0).setName(name);
		cities.get(0).setTraffic(traffic);
		cities.get(0).setPerConsume(perConsume);
		cities.get(0).setPrettyMonth(prettyMonth);
		cities.get(0).setWay(way);
		cities.get(0).setMap(map);
		cityService.saveOrUpdate(cities.get(0));
		return "background/success";
	}
	@RequestMapping("addCity")
	public String addCity(HttpServletRequest req){
		String country_name = req.getParameter("country");
		PropertyFilter filtersf = new PropertyFilter("country", country_name);
		List<Foreign> foreigns = foreignService.search(filtersf);
		if (foreigns.size()==0) {
			return "background/failure";
		}
		String province_name = req.getParameter("province");
		PropertyFilter filtersp = new PropertyFilter("name", province_name);
		List<Province> provinces = provinceService.search(filtersp);
		if (provinces.size()==0) {
			return "background/failure";
		}
		String city_name = req.getParameter("city");
		int prettyMonth = Integer.parseInt(req.getParameter("prettyMonth"));
		String way = req.getParameter("way");
		String dress = req.getParameter("dress");
		String language = req.getParameter("language");
		String history = req.getParameter("history");
		String custom = req.getParameter("custom");
		String consumeLevel = req.getParameter("consumeLevel");
		String traffic = req.getParameter("trafic");
		String hotView = req.getParameter("hotView");
		String map = req.getParameter("map");
		City city = new City();
		city.setForeign(foreigns.get(0));
		city.setProvince(provinces.get(0));
		city.setName(city_name);
		city.setPrettyMonth(prettyMonth);
		city.setWay(way);
		city.setDress(dress);
		city.setLanguage(language);
		city.setHistory(history);
		city.setCustom(custom);
		city.setConsumeLevel(consumeLevel);
		city.setTraffic(traffic);
		city.setHotView(hotView);
		city.setMap(map);
		cityService.saveOrUpdate(city);
		return "background/success";
	}
//	@RequestMapping("addCity")
//	public String addCity(City city){
//		cityService.saveOrUpdate(city);
//		return "background/success";
//	}
	@RequestMapping("delCity")
	public String delCity(HttpServletRequest req){
		int id = Integer.parseInt(req.getParameter("id"));
		cityService.remove(id);
		return "background/successDel";
	}
	@RequestMapping("{pageNo}")
	public String list(@PathVariable int pageNo,Model model){
		Page<City> page = new Page<City>(5);
		page.setPageNo(pageNo);
		cityService.loadAll(page);
		model.addAttribute("page", page);
		return "background/findCity";
	}
}
